package me.lovatcat.springboot.demo.web.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.lovatcat.springboot.demo.domain.po.User;
import me.lovatcat.springboot.demo.service.IUserService;
import me.lovatcat.springboot.demo.vo.UserAddVo;
import me.lovatcat.springboot.demo.vo.UserUpdateVo;
import me.lovetcat.common.dto.RespDto;
import me.lovetcat.common.util.WebUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author acat on 2017-01-10 16:13
 */
@Api(value = "users", description = "用户相关接口")
@RestController
public class UserController {

    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "获取用户列表", notes = "测试RESTful Api GET")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "页码", name = "pageNum", dataType = "int", paramType = "path", required = true),
        @ApiImplicitParam(value = "页数据量", name = "pageSize", dataType = "int", paramType = "path", required = true)
    })
    @GetMapping("/users/{pageNum}/{pageSize}")
    public RespDto<PageInfo<User>> getPage(@PathVariable int pageNum, @PathVariable int pageSize){
        PageInfo<User> page = userService.getPage(pageNum, pageSize);
        return RespDto.ok(page);
    }

    /**
     * 采用BindingResult方式进行入参校验
     * @param userVo
     * @return
     */
    @ApiOperation(value = "添加用户", notes = "测试RESTful Api POST, from方式传参")
    @PostMapping("/user")
    public RespDto addUser(@RequestBody @Validated UserAddVo userVo, BindingResult validResult){
        User suser = userService.getByMobile(userVo.getMobile());
        if(suser != null){
            validResult.rejectValue("mobile", "misFormat","该手机号码已经注册过了");
        }
        if(validResult.hasErrors()){
            return RespDto.fail_valid(WebUtil.setFieldValidErrorMsgs(validResult.getFieldErrors()));
        }
        User user = new User();
        BeanUtils.copyProperties(userVo, user);
        userService.insert(user);
        return RespDto.ok("用户添加成功");
    }

//    /**
//     * 采用异常处理进行参数校验
//     * @param userVo
//     * @return
//     */
//    @ApiOperation(value = "添加用户", notes = "测试RESTful Api POST")
//    @PostMapping("/user")
//    public RespDto addUser(@RequestBody @Validated UserAddVo userVo){
//        User user = new User();
//        BeanUtils.copyProperties(userVo, user);
//        userService.insert(user);
//        return RespDto.ok("用户添加成功");
//    }

    @ApiOperation(value = "删除用户", notes = "测试RESTful Api DELETE")
    @ApiImplicitParam(value = "用户id", name = "id", dataType = "int", paramType = "path", required = true)
    @DeleteMapping("/user/{id}")
    public RespDto deleteUser(@PathVariable Integer id){
        userService.delete(id);
        return RespDto.ok();
    }

    @ApiOperation(value = "修改用户", notes = "测试RESTful Api PUT, json方式传参")
    @PutMapping("/user")
    public RespDto updateUser(@RequestBody UserUpdateVo userVo){
        User user = new User();
        BeanUtils.copyProperties(userVo, user);
        userService.update(user);
        return RespDto.ok();
    }

    @ApiOperation(value = "查询用户", notes = "测试RESTful Api GET")
    @ApiImplicitParam(value = "用户id", name = "id", dataType = "int", paramType = "path", required = true)
    @GetMapping("/user/{id}")
    public RespDto<User> get(@PathVariable Integer id){
        User user = userService.get(id);
        return RespDto.ok(user);
    }

}
